package com.shijunhao.graduation.service.business.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shijunhao.graduation.domain.entity.*;
import com.shijunhao.graduation.domain.vo.CurrentUserVo;
import com.shijunhao.graduation.enums.DictCode;
import com.shijunhao.graduation.exception.BusinessException;
import com.shijunhao.graduation.mapper.BusinessUserInfoMapper;
import com.shijunhao.graduation.properties.RoleParams;
import com.shijunhao.graduation.service.business.*;
import com.shijunhao.graduation.service.system.IOrgUserService;
import com.shijunhao.graduation.service.system.ISystemDictService;
import com.shijunhao.graduation.service.system.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-08
 */
@Service
public class BusinessUserInfoServiceImpl extends BaseServiceImpl<BusinessUserInfoMapper, BusinessUserInfo> implements IBusinessUserInfoService {

    @Resource
    private IOrgUserService orgUserService;

    @Resource
    private ISystemDictService systemDictService;

    @Resource
    private IBusinessNoteService businessNoteService;

    @Resource
    private IBusinessNoteDiscussService businessNoteDiscussService;

    @Resource
    private IBusinessForumService businessForumService;

    @Resource
    private IBusinessForumDiscussService businessForumDiscussService;

    @Resource
    private IBusinessUserGoodService userGoodService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(BusinessUserInfo dto) {
        CurrentUserVo user = getCurrentOrgUser();
        BusinessUserInfo info = getById(dto.getId());
        if (!user.getUser().getId().equals(info.getCreateBy()) && !RoleParams.ADMIN.equals(user.getRole())) {
            throw new BusinessException("无修改权限");
        }
        BeanUtils.copyProperties(dto, info);
        return updateById(info);
    }

    @Override
    public CurrentUserVo toVo(BusinessUserInfo entity) {
        CurrentUserVo result = new CurrentUserVo();
        OrgUser user = orgUserService.getById(entity.getUid());
        result.setUser(user);
        result.setUserInfo(entity);
        SystemDictItem systemDictItem = systemDictService.loadByCodeValue(DictCode.ROLE.getCode(), user.getRole());
        result.setRole(systemDictItem.getDictLabel());
        return result;
    }

    @Override
    public CurrentUserVo getCurrentOrgUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null)
            return new CurrentUserVo();
        CurrentUserVo cur = new CurrentUserVo();
        //账户信息
        String currentPrincipalName = authentication.getName();
        OrgUser orgUser = orgUserService.loadUserByName(currentPrincipalName);
        cur.setUser(orgUser);
        if (orgUser != null) {
            //用户信息
            BusinessUserInfo info = getOne(Wrappers.lambdaQuery(BusinessUserInfo.class).eq(BusinessUserInfo::getUid, orgUser.getId()));
            cur.setUserInfo(info);
            //角色信息
            SystemDictItem role = systemDictService.loadByCodeValue(DictCode.ROLE.getCode(), orgUser.getRole());
            cur.setRole(role.getDictLabel());
        }
        return cur;
    }

    @Override
    public OrgUser getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null)
            return null;
        //账户信息
        String currentPrincipalName = authentication.getName();
        return orgUserService.loadUserByName(currentPrincipalName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        //删除文章及评论
        businessNoteService.deleteByCondition(Wrappers.lambdaQuery(BusinessNote.class).eq(BaseEntity::getCreateBy,id));
        businessForumService.deleteByCondition(Wrappers.lambdaQuery(BusinessForum.class).eq(BaseEntity::getCreateBy,id));
        businessNoteDiscussService.deleteByCondition(Wrappers.lambdaQuery(BusinessNoteDiscuss.class).eq(BaseEntity::getCreateBy,id));
        businessForumDiscussService.deleteByCondition(Wrappers.lambdaQuery(BusinessForumDiscuss.class).eq(BaseEntity::getCreateBy,id));
        userGoodService.deleteByCondition(Wrappers.lambdaQuery(BusinessUserGood.class).eq(BaseEntity::getCreateBy,id));

        //删除个人信息账号信息
        this.deleteByCondition(Wrappers.lambdaQuery(BusinessUserInfo.class).eq(BusinessUserInfo::getUid,id));
        orgUserService.deleteById(id);
        return true;
    }
}
